package Common;

import Dto.SerializableDTO;
import Util.ClickNumbersDispatcher;
import Util.OutputAndInputFile;

import java.io.IOException;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.Set;
import Enum.ClickEnums;

public class AutoClick {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {


        synchronized ("lock") {
            //对准IJ下边框，手机屏幕左边框对准屏幕边界
            OutputAndInputFile IOUtil = new OutputAndInputFile();
            SerializableDTO dto = (SerializableDTO) IOUtil.tryReadFile();
            try {
                Set<Integer> password = new HashSet<>();
                Set<Integer> TriedPassword = dto.getTriedPasswords();
                System.out.println(dto.getTriedPasswords());
                for (int i = 0; i < 10000; i++) {
                    if (TriedPassword.contains(i)) {
                        continue;
                    }
                    password.add(i);
                }

                Scanner sc = new Scanner(System.in);
                System.err.println("上次最后尝试的密码是：" + dto.getLastTriedPassword() + "\njiang个人规则为：升序尝试");
                int stateControl;

                while (true) {
                    try {
                        //状态控制, 执行次数
                        System.out.println("输入尝试的次数：（-1为无限次）");
                        stateControl = sc.nextInt();
                        if (stateControl == -1) {
                            break;
                        }
                        if (stateControl >= 10000 || stateControl < 0) {
                            throw new InputMismatchException();
                        }
                        System.out.println("准备尝试：" + stateControl + "次！");
                        break;
                    } catch (InputMismatchException e) {
                        System.err.println("只能输入 0 -- 10000 间的数字！");
                    } finally {
                        sc.nextLine();
                    }
                }

                stateControl--;
                System.out.println("等待5s调节");
                Thread.sleep(5 * 1000);

                int tryCount = 1;
                if (stateControl == -1) {
                    int count = password.size();
                    for (int nowPassword : password) {
                    /*if(TriedPassword.contains(nowPassword)){
                        System.err.println("此密码已经尝试过！！");
                        System.err.println("—————————————————重新输入———————————————————————");
                        continue;
                    }*/
                        ClickEnums type = ClickEnums.AIR_DROID;
                        ClickNumbersDispatcher.DispatcherHolder.getInstance().getClickNumber(type).doClickNumber(nowPassword);

//                        password.remove(nowPassword);
                        count--;
                        TriedPassword.add(nowPassword);
                        dto.setLastTriedPassword(nowPassword);
                        IOUtil.tryOutFile(dto);


                        System.out.println("———————————————此密码未尝试！！！本次成功完成, 剩余" + count + "个密码未尝试—————————————————");
                        if (tryCount++ == 5) {
                            tryCount = 0;
                            System.err.println("等待60s的密码间隔");
                            Thread.sleep(60 * 1000);
                        }

                    }
                } else {
                    int count = password.size();
                    for (int nowPassword : password) {
                        /*if (TriedPassword.contains(nowPassword)) {
                            System.err.println("此密码已经尝试过！！");
                            System.err.println("—————————————————重新输入———————————————————————");
                            continue;
                        }*/
                        ClickEnums type = ClickEnums.AIR_DROID;
                        ClickNumbersDispatcher.DispatcherHolder.getInstance().getClickNumber(type).doClickNumber(nowPassword);

//                        password.remove(nowPassword);
                        count--;
                        TriedPassword.add(nowPassword);
                        dto.setLastTriedPassword(nowPassword);

                        System.out.println("———————————————此密码未尝试！！！本次成功完成, 剩余" + count + "个密码未尝试—————————————————");

                        if (stateControl-- == 0) {
                            IOUtil.tryOutFile(dto);
                            System.err.println("到达次数！");
                            break;
                        }
                        if (tryCount++ == 5) {
                            tryCount = 0;
                            System.err.println("等待60s的密码间隔");
                            Thread.sleep(63 * 1000);
                        }
                    }
                }

            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                IOUtil.tryOutFile(dto);
                System.out.println("\n成功输出已尝试过的密码");
            }

        }
    }
}
